﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class IdNameService : IIdNameService
    {
        public long AddNew(string typeName, string name)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                IdNamesEntity en = new IdNamesEntity();
                en.Name = name;
                en.TypeName = typeName;
                ct.IdNames.Add(en);
                ct.SaveChanges();
                return en.Id;
            }
        }
        private IDNameDTO ToDTO(IdNamesEntity e)
        {
            IDNameDTO dto = new IDNameDTO();
            dto.CreateTime = e.CreateDatatime;
            dto.Id = e.Id;
            dto.Name = e.Name;
            dto.TypeName = e.TypeName;
            return dto;
        }
        public IDNameDTO[] GetAll(string typeName)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<IdNamesEntity> cs = new CommonService<IdNamesEntity>(ct);
                return cs.GetAll().Where(i => i.TypeName == typeName).ToList().Select(i => ToDTO(i)).ToArray();
            }
        }

        public IDNameDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<IdNamesEntity> cs = new CommonService<IdNamesEntity>(ct);
                IdNamesEntity e = cs.GetById(id);
                if (e == null)
                {
                    throw new ArgumentException("没有这个typename");
                }
                return ToDTO(e);
            }
        }
    }
}
